package Solucion;

public class Jut {
	
	
	public static String jutADecimal(String valorJUT){
		int ret = 0;
		
		if(!cadenaValida(valorJUT)) return valorJUT;
		
		int iteradorCifras = valorJUT.length();
		
		for ( int i=0; i<valorJUT.length(); i++ ){
			int n = Integer.valueOf(valorJUT.substring(i, i+1));
			ret += n*obtenerFactorial(iteradorCifras);
			iteradorCifras--;
		}		
		return String.valueOf(ret);
	}
	
	private static boolean cadenaValida(String s){
		//Reemplazo simbolo de signo
		String original = s;
		s = (s == null) ? null : s.replace("-", "").replace("+", "");
		
		if ( s == null || s.length() > 5 || s.length() == 0 ) return false;
		try{
			int n = Integer.valueOf(original);
			if ( n < 0 ) return false;
		}catch(NumberFormatException e){
			return false;
		}
		return true;
	}
	
	private static int obtenerFactorial(int n){
		//caso base
		if (n == 1 || n == 0) return 1;
				
		return n*(obtenerFactorial(n-1));
	}

}
